//
//  YDYOldHomeViewController.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/9/20.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYOldHomeViewController.h"
#import "YDYTopView.h"
#import "YDYSliderViewController.h"
#import "YDYMainScroll.h"
#import "YDYFenQuViewController.h"
#import "YDYFenQuLayout.h"
#import "YDYSearchView.h"
@interface YDYOldHomeViewController ()<YDYTopViewDelegate,UIScrollViewDelegate>

/** 分区控制器 */
@property (nonatomic, strong) YDYFenQuViewController *fenquVC;

/** 顶部视图的数据源*/
@property (nonatomic, strong) NSArray *titleArray;
//顶部视图
@property (weak, nonatomic) IBOutlet YDYTopView *topView;
@property (weak, nonatomic) IBOutlet YDYMainScroll *mainScrollView;

/** 侧拉栏 */
@property (nonatomic, strong) YDYSliderViewController *sliderViewVc;
/** 侧拉栏是否已经弹出 */
@property (nonatomic, assign) BOOL sliderIsshow;
@end

@implementation YDYOldHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.topView.titleArray = self.titleArray;
    self.topView.delegate = self;
    self.mainScrollView.contentSize = CGSizeMake(self.titleArray.count * YDYScreenSize.width, 0);
    
    [self setupNoti];
    [self setupSliderView];
    [self addChildViewControllers];
   
}
-(void)addChildViewControllers{
    [self addChildViewController:[[YDYFenQuViewController alloc]init]];
    
}

-(void)setupSliderView{
    
    //用于监听用户从边缘滑进来的
    UIView *slider = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, YDYScreenSize.height)];
    slider.backgroundColor = [UIColor clearColor];
//    slider.backgroundColor = [UIColor blackColor];
    [self.view addSubview:slider];
    
    UIGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(sliderViewPanGesture:)];
    [slider addGestureRecognizer:pan];
    
}
-(void)setupNoti{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showSlider) name:ydy_showSliderViewNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showSearchView) name:ydy_showSearchViewNotification object:nil];
}
-(void)sliderViewPanGesture:(UIPanGestureRecognizer *)gesture{
    self.sliderViewVc.panGesture = gesture;
     [self.view addSubview:self.sliderViewVc.view];
    
    
    __weak typeof(self) weakself = self;
    self.sliderViewVc.pushback = ^(UIViewController *vc){
        if ([vc isKindOfClass:[YDYSliderViewController class]]) {
            weakself.sliderIsshow = NO;
            weakself.sliderViewVc = nil;
            
        }
    };
}
-(void)showSlider{
    [self.sliderViewVc show];

}
-(void)showSearchView{
    
    YDYSearchView *searchV = [YDYSearchView searchView];
    searchV.alpha = 0;
    searchV.frame = self.view.bounds;
    [self.view addSubview:searchV];
    [UIView animateWithDuration:0.5 animations:^{
        searchV.alpha = 1;
    }];
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark -
#pragma mark -YDYTopViewDelegate
-(void)titleBtndidClick:(UIButton *)btn{
    NSString *title = btn.titleLabel.text;
    CGFloat offset = YDYScreenSize.width * (btn.tag -1);
    [self.mainScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    //加载视图
    switch (btn.tag) {
        case 1:
            //是番剧
            break;
        case 2:
            //推荐
            break;
        case 3:
            //分区
            self.fenquVC.view.frame = CGRectMake(offset, 0, YDYScreenSize.width, self.mainScrollView.frame.size.height);
            [self.mainScrollView addSubview:self.fenquVC.view];
            break;
            
        default:
            break;
    }
    
    
    YDYLog(@"你点击了-%@",title);
    
    
}


#pragma mark -
#pragma mark -uiscrollerViewDlegate{
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int i  = scrollView.contentOffset.x / YDYScreenSize.width;
    self.topView.seletedTag = i;
    YDYLog(@"%d",i);
}
#pragma mark -
#pragma mark -懒加载
-(NSArray *)titleArray{
    if (_titleArray.count == 0) {
        _titleArray = @[@"番剧",@"推荐",@"分区",@"关注",@"发现"];
    }
    return _titleArray;
}

-(YDYSliderViewController *)sliderViewVc{
    if (_sliderViewVc == nil) {
        _sliderViewVc = [[YDYSliderViewController alloc]init];
        [self.view addSubview:_sliderViewVc.view];
        
    }
    return _sliderViewVc;
}
-(void)setSliderIsshow:(BOOL)sliderIsshow{
    _sliderIsshow = sliderIsshow;
}
-(YDYFenQuViewController *)fenquVC{
    if (_fenquVC == nil) {
        _fenquVC = [[YDYFenQuViewController alloc]init];
//        _fenquVC = [[YDYFenQuViewController alloc]initWithCollectionViewLayout:[[YDYFenQuLayout alloc]init]];
    }
    return _fenquVC;
}

@end
